package com.augurit.gzcgwsp.qyzz.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
import jxl.read.biff.BiffException;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;

import com.augurit.gzcgwsp.excelimportexport.util.ExcelUtil;
import com.augurit.gzcgwsp.qyzz.dao.YwRqQyzzEquipmentDao;
import com.augurit.gzcgwsp.qyzz.entity.YwRqRyzzEquipment;
import com.augurit.gzcgwsp.qyzz.entity.YwRqRyzzFinalEquipment;
import com.augurit.gzcgwsp.qyzz.entity.YwRqRyzzFinalOtherEvidence;
import com.augurit.gzcgwsp.qyzz.entity.YwRqRyzzOtherEvidence;
import com.augurit.gzcgwsp.qyzz.service.IYwRqQyzzEquipmentService;
import com.augurit.gzcgwsp.qyzz.service.IYwRqQyzzFinalEquipmentService;

/**
 * 
 *  企业资质--企业主要设备与检测设备service实现类
 * @author 张军
 *
 */
@Service
@Transactional
public class YwRqQyzzAzEquipmentServiceImpl  implements IYwRqQyzzEquipmentService {
	private final static Log log=LogFactory.getLog(YwRqQyzzAzEquipmentServiceImpl.class);
	 @Autowired
	private YwRqQyzzEquipmentDao ywRqQyzzEquipmentDao;
      @Autowired
    private IYwRqQyzzFinalEquipmentService ywRqQyzzFinalEquipmentService;
	public void saveEntity(YwRqRyzzEquipment entity) {
		ywRqQyzzEquipmentDao.save(entity);
		
	}


	/*
	 * 导入企业主要设备与检测设备
	 * @see com.augurit.gzcgwsp.qyzz.service.IYwRqQyzzJsRyService#importQyzzInfo(jxl.Sheet, java.lang.String, long, java.lang.String)
	 */
public String importQyzzEquipment(Sheet sheet, String acceptNo, long qyid,
		String username,Map map) {
	String errors="";
	String process=(String) map.get("process");
	if("cksa".equals(process)){
//	    
//	    List<YwRqRyzzFinalEquipment> list=ywRqQyzzFinalEquipmentService.getEquipments(qyid,acceptNo);
//        if(list!=null && list.size()>0){
//            for(YwRqRyzzFinalEquipment entity:list){
//                //先删除
//                ywRqQyzzFinalEquipmentService.delete(entity);
//            }
//        }
        //新增
        int rows=sheet.getRows();
        for(int i=2;i<rows;i++){
            String ename=ExcelUtil.readData(sheet,1, i);//设备名称
            String xh=ExcelUtil.readData(sheet, 2, i);//型号
            String count=ExcelUtil.readData(sheet,3, i);//数量
            if(isHasData(ename,xh,count)){
                YwRqRyzzFinalEquipment entity=new YwRqRyzzFinalEquipment();
                entity.setQyid(qyid);
                entity.setEname(ename);
                entity.setXh(xh);
               
                try {
                	 entity.setEcount(Integer.valueOf(count));
                    ywRqQyzzFinalEquipmentService.saveEntity(entity);
                } catch (Exception e) {
                    log.error("第四个工作表"+"第"+i+"行出现错误"+e);
                    errors+="第四个工作表"+"第"+i+"行出现错误\n";               
                }
            }
        }
	}else{
	    List<YwRqRyzzEquipment> list=ywRqQyzzEquipmentDao.getEquipments(qyid,acceptNo);
	    if(list!=null && list.size()>0){
	        for(YwRqRyzzEquipment entity:list){
	            //先删除
	            ywRqQyzzEquipmentDao.delete(entity);
	        }
	    }
	    //新增
	    int rows=sheet.getRows();
	    for(int i=2;i<rows;i++){
	        String ename=ExcelUtil.readData(sheet,1, i);//设备名称
	        String xh=ExcelUtil.readData(sheet, 2, i);//型号
	        String count=ExcelUtil.readData(sheet,3, i);//数量
	        if(isHasData(ename,xh,count)){
	            YwRqRyzzEquipment entity=new YwRqRyzzEquipment();
	            entity.setAcceptNo(acceptNo);//受理编号
	            entity.setQyid(qyid);
	            entity.setEname(ename);
	            entity.setXh(xh);
	            entity.setEcount(Integer.valueOf(count));
	            try {
	                ywRqQyzzEquipmentDao.save(entity);
	            } catch (Exception e) {
	                log.error("第四个工作表"+"第"+i+"行出现错误"+e);
	                errors+="第四个工作表"+"第"+i+"行出现错误\n";               
	            }
	        }
	    }
	}
	

	return errors;
}
	
	/**
	 * 判断是否有数据
	 * @param name
	 * @param xh
	 * @param zsbh
	 * @param fzbm
	 * @return
	 */
	public boolean isHasData(String name,String xh,String zsbh){
		if(StringUtils.isNotBlank(name)&&StringUtils.isNotBlank(zsbh))
			return true;
		else 
			return false;
		
	}
	
	
	  public static void main(String[] args) throws BiffException, IOException{
		  String filepath="c:\\安装维修企业资质申请表922.xls";
		   File file=new File(filepath);
			Workbook wb = Workbook.getWorkbook(file);
			Sheet sheet=wb.getSheet(2);
			int rowss=sheet.getRows();
			System.out.print(rowss);
			String dwmc=ExcelUtil.readData(sheet,1, 2);//单位名称
			System.out.print(dwmc);
	   }


	/*
	 * 填充企业主要设备与检测设备
	 * @see com.augurit.gzcgwsp.qyzz.service.IYwRqQyzzEquipmentService#copyWritableSheet(jxl.write.WritableSheet, java.lang.String, long)
	 */
	public void copyWritableSheet(WritableSheet ws, String acceptNo,
			long qyid,String type) {
	    if("qyxx".equals(type)){
	        List<YwRqRyzzFinalEquipment> list=ywRqQyzzFinalEquipmentService.getEquipments(qyid, acceptNo);
	        for(int i=0;i<list.size();i++){
	            YwRqRyzzFinalEquipment entity=list.get(i);
	            String xh=entity.getXh();//型号
	            String ename=entity.getEname();//姓名
	            String count=entity.getEcount()+"";//数量
	            try {
	                WritableCellFormat wcf = new WritableCellFormat();
	                wcf.setBorder(Border.ALL, BorderLineStyle.THIN);
	                wcf.setAlignment(Alignment.CENTRE);
	                wcf.setVerticalAlignment(VerticalAlignment.CENTRE); 
	                ExcelUtil.modiStrCell2(ws, 0,  i+2, i+1+"", wcf);
	                ExcelUtil.modiStrCell2(ws, 1,  i+2, ename, wcf);
	                ExcelUtil.modiStrCell2(ws, 2,  i+2, xh, wcf);
	                ExcelUtil.modiStrCell2(ws, 3,  i+2, count, wcf);
	            } catch (Exception e) {
	                log.error("受理编号"+acceptNo+"导出主要设备与检测设备错误"+e);
	            } 
	     }
	    }else{
	           List<YwRqRyzzEquipment> list=ywRqQyzzEquipmentDao.getEquipments(qyid,acceptNo);
	            for(int i=0;i<list.size();i++){
	                YwRqRyzzEquipment entity=list.get(i);
	                String xh=entity.getXh();//型号
	                String ename=entity.getEname();//姓名
	                String count=entity.getEcount()+"";//数量
	                try {
	                    WritableCellFormat wcf = new WritableCellFormat();
	                    wcf.setBorder(Border.ALL, BorderLineStyle.THIN);
	                    wcf.setAlignment(Alignment.CENTRE);
	                    wcf.setVerticalAlignment(VerticalAlignment.CENTRE); 
	                    ExcelUtil.modiStrCell2(ws, 0,  i+2, i+1+"", wcf);
	                    ExcelUtil.modiStrCell2(ws, 1,  i+2, ename, wcf);
	                    ExcelUtil.modiStrCell2(ws, 2,  i+2, xh, wcf);
	                    ExcelUtil.modiStrCell2(ws, 3,  i+2, count, wcf);
	                } catch (Exception e) {
	                    log.error("受理编号"+acceptNo+"导出主要设备与检测设备错误"+e);
	                } 
	            
	         }
	    }

		
	}


	/*
	 * 获取设备信息
	 * @see com.augurit.gzcgwsp.qyzz.service.IYwRqQyzzEquipmentService#getQyzzEquipment(java.lang.String, long)
	 */
	public List<YwRqRyzzEquipment> getQyzzEquipment(String acceptNo, long qyid) {
		return ywRqQyzzEquipmentDao.getEquipments(qyid,acceptNo);
	}


	/*
	 * 获取实体
	 * @see com.augurit.gzcgwsp.qyzz.service.IYwRqQyzzEquipmentService#getEntity(long)
	 */
	public YwRqRyzzEquipment getEntity(long id) {
		return ywRqQyzzEquipmentDao.get(id);
		
	}

/*
 * (non-Javadoc)
 * @see com.augurit.gzcgwsp.qyzz.service.IYwRqQyzzEquipmentService#importFinalData(java.lang.Long, java.lang.String)
 */
    public Map importFinalData(Long qyid, String acceptNo) {
        List<YwRqRyzzEquipment> list=this.getQyzzEquipment(acceptNo, qyid);
        for(YwRqRyzzEquipment entity:list){
            YwRqRyzzFinalEquipment finalEntity = new DozerBeanMapper().map(entity, YwRqRyzzFinalEquipment.class);
            finalEntity.setId(null);
            this.ywRqQyzzFinalEquipmentService.saveEntity(finalEntity);
        }
        return null;
    }


    /**
     * 分页查询
     */
	public Page<YwRqRyzzEquipment> search(Page<YwRqRyzzEquipment> page, Map map) {
		String acceptNo=(String) map.get("acceptNo");
		String hql=" from "+YwRqRyzzEquipment.class.getName()+" where acceptNo='"+acceptNo+"'";
		Map<String,String> values=new HashMap<String,String>();
		values.put("acceptNo", acceptNo);
		page=ywRqQyzzEquipmentDao.findPage(page, hql, values);
		return page;
	}


	public Page getPage(String acceptNo, Page page, Long qyid, String type) {
		String hql=" from "+YwRqRyzzEquipment.class.getName()+" where acceptNo='"+acceptNo+"'";
		if("2".equals(type)){
			hql=" from "+YwRqRyzzFinalEquipment.class.getName()+" where qyid='"+qyid+"'";
		}else if("cl".equals(type)){
			hql=" from "+YwRqRyzzOtherEvidence.class.getName()+" where acceptNo='"+acceptNo+"'";
		}else if("oldzmcl".equals(type)){
			hql=" from "+YwRqRyzzFinalOtherEvidence.class.getName()+" where qyid='"+qyid+"'";
		}
		Map<String,String> values=new HashMap<String,String>();
		page=ywRqQyzzEquipmentDao.findPage(page, hql, values);
		return page;
	}

	 
}
